package com.online.teaching.module.admin.course.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.online.teaching.dao.CourseMapper;
import com.online.teaching.dao.TakesMapper;
import com.online.teaching.dao.TeacherMapper;
import com.online.teaching.dao.TeachesMapper;
import com.online.teaching.module.admin.course.domain.dto.CourseAddDTO;
import com.online.teaching.module.admin.course.domain.dto.TakesAddDTO;
import com.online.teaching.module.admin.course.domain.dto.TeachesAddDTO;
import com.online.teaching.module.admin.course.domain.vo.TeachesGetVO;
import com.online.teaching.module.admin.course.service.AdminCourseService;
import com.online.teaching.pojo.Course;
import com.online.teaching.pojo.Takes;
import com.online.teaching.pojo.Teacher;
import com.online.teaching.pojo.Teaches;
import com.online.teaching.utils.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Id;
import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : StudentBlogServiceImpl  //类名
 * @Description :   //描述
 * @Author : YG  //作者
 * @Date: 2020-11-04 13:22  //时间
 */
@Service
public class AdminCourseServiceImpl implements AdminCourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private TeachesMapper teachesMapper;

    @Autowired
    private TakesMapper takesMapper;

    @Override
    public int addCourse(CourseAddDTO courseAddDTO) {
        Course copy = SmartBeanUtil.copy(courseAddDTO, Course.class);
        int i = courseMapper.insertSelective(copy);
        return i;
    }

    @Override
    public int deleteCourse(Integer courseId) {
        return courseMapper.deleteCourse(courseId);
    }

    @Rollback
    @Override
    public int addTeaches(TeachesAddDTO teachesAddDTO) {
        Teaches teaches = SmartBeanUtil.copy(teachesAddDTO, Teaches.class);
        Teacher teacher = teacherMapper.selectByPrimaryKey(teaches.getTeaId());
        Course course = courseMapper.selectByPrimaryKey(teaches.getCourseId());
        if(teacher==null||course==null){
            return 0;
        }
        teaches.setCourseName(course.getCourseChName());
        teaches.setTeacherName(teacher.getTeaName());
        return teachesMapper.insertSelective(teaches);
    }

    @Override
    public int deleteTeaches(Integer teachesId) {
        Teaches teaches=new Teaches();
        teaches.setTeachesId(teachesId);
        teaches.setIsValid(1);
        return teachesMapper.updateByPrimaryKeySelective(teaches);
    }

    @Override
    public PageInfo<TeachesGetVO> getTeachesList(Integer page,Integer size) {
        List<Teaches> teaches = teachesMapper.adminFindCourse();
        List<TeachesGetVO> teachesGetVOS = SmartBeanUtil.copyList(teaches, TeachesGetVO.class);
        return new PageInfo<>(teachesGetVOS);
    }

    //随便应付一下高并发吧
    @Transactional
    @Override
    public int addTakes(TakesAddDTO takesAddDTO) {
        Teaches teaches = teachesMapper.selectByPrimaryKey(takesAddDTO.getTeachesId());
        if(teaches==null){
            return 0;
        }
        List<Takes>list=new ArrayList<>();
        for (int i = 0; i < takesAddDTO.getStudentId().size(); i++) {
            Takes takes=new Takes();
            takes.setTeachesId(takesAddDTO.getTeachesId());
            takes.setStudentId(takesAddDTO.getStudentId().get(i));
            list.add(takes);
        }
        if(list.size()==0){
            return 0;
        }
        int i = takesMapper.insertList(list);
        teaches.setTeachesStudentNum(teaches.getTeachesStudentNum()+i);
        int i1 = teachesMapper.updateByPrimaryKeySelective(teaches);
        return i1;
    }
}
